const http = require('node:http')
const url = require('node:url')
const qs = require('node:querystring')
const fs = require('node:fs')

var server = http.createServer((req,res)=>{
    var {pathname,query}= url.parse(req.url)
    if(req.method == 'GET'){
        if(pathname == '/s'){
            res.writeHead(200,{'content-type':"xxx/plain;charset=utf-8"})
            res.write('这是/s接口')
            res.end()
        }
        if(pathname == '/d'){
            res.writeHead(200,{'content-type':"xxx/plain;charset=utf-8"})
            res.write('这是/d接口')
            res.end()
        }
        if(pathname == '/a'){
           fs.readFile('./index.html','utf-8').then((data)=>{
            res.writeHead(200,{'content-type':"xxx/html;charset=utf-8"})
            res.write(data)
            res.end()
        }) 
        }
        if(pathname == '/form'){
            //接受form发送的数据
            var {query} = url.parse(req.url)
            console.log('前端给服务器发送的信息如下：' + query)
            res.writeHead(200,{'content-type':"xxx/plain;charset=utf-8"})
            res.write('这是/form接口')
            res.end()
        }
    }
    if(req.method == 'POST'){
        if(pathname == '/h'){
            res.writeHead(200,{'content-type':"xxx/plain;charset=utf-8"})
            res.write('这是/h接口')
            res.end()
        }
        if(pathname == '/form1'){
            var str = ""
            req.on('data',(chunk)=>{
                str += chunk
            })
            req.on('end',()=>{
                console.log('数据传递结束了')
                console.log(str)
                res.writeHead(200,{'content-type':"xxx/plain;charset=utf-8"})
                res.write('你的数据传输结束啦')
                res.end()
            })
        }
    }
})

server.listen(3000)